package com.lenovo.lcdm.dcm.config.mybatis;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.postgresql.util.PGobject;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

@MappedTypes(Object.class)
@MappedJdbcTypes(JdbcType.OTHER)
public class JsonTypeHandler<T> extends BaseTypeHandler<T> {
    private static final PGobject jsonObject = new PGobject();

    private static final ObjectMapper objectMapper = new ObjectMapper();

    @SneakyThrows
    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, T stringObjectMap, JdbcType jdbcType) throws SQLException {
        jsonObject.setType("json");
        jsonObject.setValue(objectMapper.writeValueAsString(stringObjectMap));
        preparedStatement.setObject(i, jsonObject);
    }

    @SneakyThrows
    @Override
    public T getNullableResult(ResultSet resultSet, String s) {
        return (T) objectMapper.readValue(resultSet.getString(s), Map.class);
    }

    @SneakyThrows
    @Override
    public T getNullableResult(ResultSet resultSet, int i) {
        return (T) objectMapper.readValue(resultSet.getString(i), Map.class);
    }

    @SneakyThrows
    @Override
    public T getNullableResult(CallableStatement callableStatement, int i) {
        return (T) objectMapper.readValue(callableStatement.getString(i), Map.class);
    }
}
